package com.zjcode.cdcer.service.impl;

import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import com.zjcode.cdcer.mapper.ZjcodeSysUserTmpMapper;
import com.zjcode.cdcer.model.ZjcodeSysUserTmp;
import com.zjcode.cdcer.service.ZjcodeSysUserTmpService;
import java.util.Date;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @ClassName:ZjcodeSysUserTmpServiceImpl
 * @功能：
 * @author：wlei
 * @日期：2023/2/7-02:37
 **/
@Service
@Slf4j
public class ZjcodeSysUserTmpServiceImpl implements ZjcodeSysUserTmpService {

    @Autowired
    private ZjcodeSysUserTmpMapper zjcodeSysUserTmpMapper;

    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Override
    public ZjcodeSysUserTmp findById(String id) {
        return zjcodeSysUserTmpMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteById(String id) {
        if (!Objects.isNull(id)) {
            return zjcodeSysUserTmpMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public void insertSysUser(ZjcodeSysUserTmp zjcodeSysUserTmp) {
        String defPwd = "Wny@2023";
        zjcodeSysUserTmp.setPassword(encoder.encode(defPwd));
        zjcodeSysUserTmp.setStatus((byte) 1);
        zjcodeSysUserTmp.setTenantCode("10001");// 当前用户的租户编码
        zjcodeSysUserTmp.setNotifyMessage((byte) 1);
        zjcodeSysUserTmp.setNotifyTodo((byte) 1);
        zjcodeSysUserTmp.setCreateTime(new Date(System.currentTimeMillis()));
        zjcodeSysUserTmp.setUpdateTime(new Date(System.currentTimeMillis()));

        // 将数据插入临时表中
        zjcodeSysUserTmpMapper.insert(zjcodeSysUserTmp);
    }

    @Override
    public void updateSysUser(ZjcodeSysUserTmp zjcodeSysUserTmp) {
        zjcodeSysUserTmpMapper.updateByPrimaryKeySelective(zjcodeSysUserTmp);
    }

    @Override
    public int selectCountByUserNameOrMobile(String username, String mobile) {
        return zjcodeSysUserTmpMapper.selectCountByUserNameOrMobile(username, mobile);
    }
}
